<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-card class="runtime-alerts">
  <mat-card-header>
    <div class="runtime-alerts__header">
      <div class="runtime-alerts__header__titles">
        <mat-card-title>Custom alerts</mat-card-title>
        <mat-card-subtitle>Set up alerting conditions for the Gateway</mat-card-subtitle>
      </div>
      <div>
        <button [disabled]="!canCreateAlert" color="primary" aria-label="Add alert" mat-raised-button (click)="createAlert.emit()">
          <mat-icon svgIcon="gio:plus"></mat-icon>Add alert
        </button>
      </div>
    </div>
  </mat-card-header>

  <gio-table-wrapper [disableSearchInput]="true" [disablePageSize]="true" [length]="alerts.length">
    <table mat-table [dataSource]="alerts" class="runtime-alerts__table" id="apiAlertsTable" aria-label="API Alerts table">
      <!-- Name column -->
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef id="name">Name</th>
        <td mat-cell *matCellDef="let alert">{{ alert.name }}</td>
      </ng-container>

      <!-- Severity Column -->
      <ng-container matColumnDef="severity">
        <th mat-header-cell *matHeaderCellDef id="severity"></th>
        <td mat-cell *matCellDef="let alert">
          <span *ngIf="alert.severity === 'INFO'" class="gio-badge-primary">{{ alert.severity }}</span>
          <span *ngIf="alert.severity === 'WARNING'" class="gio-badge-warning">{{ alert.severity }}</span>
          <span *ngIf="alert.severity === 'CRITICAL'" class="gio-badge-error">{{ alert.severity }}</span>
        </td>
      </ng-container>

      <!-- Description Column -->
      <ng-container matColumnDef="description">
        <th mat-header-cell *matHeaderCellDef id="description">description</th>
        <td mat-cell *matCellDef="let alert">{{ alert.description }}</td>
      </ng-container>

      <!-- Counters column -->
      <ng-container matColumnDef="counters">
        <th mat-header-cell *matHeaderCellDef id="counters">Last 5m / 1h / 1d / 1M</th>
        <td mat-cell *matCellDef="let alert">
          <ng-container *ngIf="!!alert.counters; else countersEmptyBlock">
            <span [matTooltip]="alert.counters['5m'] + ' during the last 5 minutes'">{{ alert.counters['5m'] }}</span>
            /
            <span [matTooltip]="alert.counters['1h'] + ' during the last 1 hour'">{{ alert.counters['1h'] }}</span>
            /
            <span [matTooltip]="alert.counters['1d'] + ' during the last 1 day'">{{ alert.counters['1d'] }}</span>
            /
            <span [matTooltip]="alert.counters['1M'] + ' during the last 1 month'">{{ alert.counters['1M'] }}</span>
          </ng-container>
          <ng-template #countersEmptyBlock> - </ng-template>
        </td>
      </ng-container>

      <!-- Last alert column -->
      <ng-container matColumnDef="lastAlert">
        <th mat-header-cell *matHeaderCellDef id="lastAlert">Last alert</th>
        <td mat-cell *matCellDef="let alert">
          <ng-container *ngIf="!!alert.last_alert_at; else lastAlertEmptyBlock">
            {{ alert.last_alert_at | date: 'short' }}
          </ng-container>
          <ng-template #lastAlertEmptyBlock> - </ng-template>
        </td>
      </ng-container>

      <!-- Last message column -->
      <ng-container matColumnDef="lastMessage">
        <th mat-header-cell *matHeaderCellDef id="lastMessage">Last message</th>
        <td mat-cell *matCellDef="let alert">
          <ng-container *ngIf="!!alert.last_alert_at; else lastAlertMessageEmptyBlock">
            {{ alert.last_alert_message }}
          </ng-container>
          <ng-template #lastAlertMessageEmptyBlock> - </ng-template>
        </td>
      </ng-container>

      <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef id="actions"></th>
        <td mat-cell *matCellDef="let alert">
          <div class="runtime-alerts__actions">
            <button
              mat-icon-button
              aria-label="Button to edit an alert"
              matTooltip="Edit an alert"
              *gioPermission="{ anyOf: ['api-alert-u'] }"
              [disabled]="true"
            >
              <mat-icon svgIcon="gio:edit-pencil"></mat-icon>
            </button>
            <button
              *gioPermission="{ anyOf: ['api-alert-d'] }"
              mat-icon-button
              aria-label="Button to delete an alert"
              matTooltip="Delete an alert"
              (click)="deleteAlert.emit(alert)"
            >
              <mat-icon svgIcon="gio:trash"></mat-icon>
            </button>
          </div>
        </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
      <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
        <td class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">No Alerts</td>
      </tr>
    </table>
  </gio-table-wrapper>
</mat-card>
